package com.buddy.summary.code.algorithm;

/**
 * 删除k个数字后，获得删除后的最小数字
 */
public class Algorithm_08 {

    public static String removeDigits(String num,int k){
        //新整数的长度 = 原整数长度 - k
        int newLength = num.length() - k;
        //创建一个栈，用于接收所有的数字
        char[] stack = new char[num.length()];
        int top = 0;
        for (int i=0;i<num.length();++i){
            //遍历当前数字
            char c = num.charAt(i);
            while (top > 0 && stack[top - 1] > c && k>0){
                top -= 1;
                k -= 1;
            }
            //遍历到的当前数字入栈
            stack[top++] = c;
        }
        int offset = 0;
        while (offset < newLength && stack[offset] == '0'){
            offset++;
        }
        return offset == newLength ? "0":new String(stack,offset,newLength - offset);
    }

    public static void main(String[] args) {
        System.out.println(removeDigits("1593212",3));
    }
}
